//
//  KeShiKaoQingView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/5/31.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "KeShiKaoQingView.h"
#import "SYRequestAPI+Teach.h"
#import "CJYearMonthSelectedView.h"
#import "CHAlertView.h"
@implementation KeShiKaoQingView
- (instancetype)initWithFrame:(CGRect)frame andModel:(BanJiKeShiModel *)model andTopModel:(nonnull BanJiListTopDetailModel *)topModel{
    if ([super initWithFrame:frame]) {
        self.mainModel = topModel;
        self.keShiModel = model;
        [self initSubView];
        [self reloadData];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = [UIColor whiteColor];
    pageSize = 30;
    pageIndex = 1;
    beginDate = @"";
    endDate = @"";
    _dataArray = [NSMutableArray new];
    self.backgroundColor = MAINBACKCOLOR;
    [self addSubview:self.backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
        make.height.equalTo(@24);
        make.width.equalTo(@60);
    }];
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
        make.top.equalTo(@44);
    }];
    [self addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.backButton.mas_centerY);
        make.width.equalTo(@120);
        make.height.equalTo(@20);
    }];
    
    [_tableList headerRefreshBlock:^{
        self->pageIndex = 1;
        [self->_dataArray removeAllObjects];
        [self->_tableList.mj_header beginRefreshing];
        [self getBanJiKaoQingDetailRequest];
    }];
    [_tableList fooderRefreshBlock:^{
        [self->_tableList.mj_footer beginRefreshing];
        [self getBanJiKaoQingDetailRequest];
    }];
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"课时考勤";
        label.textAlignment = 1;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = TITLECOLOR;
        _titleLabel = label;
    }
    return _titleLabel;
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        [tableView registerClass:[KeShiKaoQingCell class] forCellReuseIdentifier:@"KeShiKaoQingCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _headerView = [[KeShiKaoQingTopView alloc]init];
        _headerView.frame = CGRectMake(0, 0,self.mainw, 140);
        tableView.tableHeaderView = _headerView;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KeShiKaoQingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeShiKaoQingCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[KeShiKaoQingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeShiKaoQingCell"];
    }
    KeShiKaoQingXiangQingModel *model = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    //添加查看方法
    cell.checkButton.tag = indexPath.row+100;
    [cell.checkButton addTarget:self action:@selector(checkReson:) forControlEvents:UIControlEventTouchUpInside];
     [cell reloadView];
    for (int i = 0; i<7; i++) {
        UILabel *label = (UILabel *)[cell viewWithTag:20+i];
        if (i == 0) {
            label.text = model.serial;
        }else if(i == 1){
            label.text = model.name;
        }else if (i == 2){
//            if ([model.sex isEqualToString:@"1"]) {
//                label.text = @"男";
//            }else if ([model.sex isEqualToString:@"2"]){
//                label.text = @"女";
//            }else{
//                label.text = @"不确定";
//            }
            label.text = model.sex;
        }else if (i == 3){
            label.text = model.monther;
        }else if (i == 4){
            label.text = model.montherPhone;
        }else if (i == 5){
            label.text = model.statusName;
        }else if (i == 6){
            label.text = model.tryStatusName;
        }
    }
    return cell;
}
- (void)checkReson:(UIButton *)sender{
    int tag = (int)sender.tag - 100;
    KeShiKaoQingXiangQingModel *model = _dataArray[tag];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:model.statusName message:model.leaveReason preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confir = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:confir];

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:NO completion:nil];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[SJXQHeaderTitleView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"性别",@"联系人",@"电话",@"出勤状态",@"状态",@"原因"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    return _titleView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@" 返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = button;
    }
    return _backButton;
}
- (void)back:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(APP_WIDTH, 0, APP_WIDTH, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)reloadData{
    _headerView.titleLabel.text = _mainModel.className;
    [_headerView setXueHaoLabelValue:_mainModel.schedule];
    [_headerView setKeChengLabelValue:_courseName];
    [_headerView setRenKeLaoShiLabelValue:_mainModel.headmaster];
    [_headerView setJiaoShiLabelValue:[NSString stringWithFormat:@"%@ %@",_mainModel.campusName,_mainModel.classroom]];
    [_headerView setShangKeShiJianLabelValue:_mainModel.openDate];
    [_headerView setKeMuLabelValue:_mainModel.className];
    [_headerView setKaoQingLabelValue:[NSString stringWithFormat:@"%@/%@",_mainModel.maxStudents,_mainModel.maxStudents]];
    [self getBanJiKaoQingDetailRequest];
}
#pragma mark - 班级课时考勤详情Rrequest
- (void)getBanJiKaoQingDetailRequest{
    NSDictionary *parama = @{@"classId":_mainModel.classId,
                             @"lessonId":_keShiModel.lessonId,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
   // NSLog(@"得到班级考勤详情数据上传的单数 %@",parama);
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getClassServiceClassLessonAttendList:parama success:^(id  _Nonnull responseObject) {
      //  NSLog(@"得到课时考勤详情数据 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            KeShiKaoQingXiangQingModel *model = [KeShiKaoQingXiangQingModel yy_modelWithJSON:dic];
            [self->_dataArray addObject:model];
        }
        if (array.count>0) {
            self->pageIndex ++;
        }
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
        [self->_tableList.mj_footer endRefreshing];
        [self->_tableList.mj_header endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [self->_tableList.mj_footer endRefreshing];
        [self->_tableList.mj_header endRefreshing];
    }];
}

@end
